#ifndef USER_H
#define USER_H
#include <QString>
#include "Enums.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QList>
class User
{
private:

public:
    QString name;
    QString id;
    Role type;
    QString avatar;
    QString email;
    QString idcard_num;
    QString pass;
    QDate join_at;
    int age;
    QString tel;
    User();
    User(const User& other) = default;
    User(QString name,QString password,QString idcard_num,QString email, Role type,int age,QDate join = QDate::currentDate()):name(name),type(type),email(email),idcard_num(idcard_num),pass(password),join_at(join),age(age){}
    virtual QJsonObject toJson();
    static QString typeToString(Role);
    QString getName()const{return name;}
    QString getId()const{return id;}
    QString getIdCard()const{return idcard_num;}
    static Role StringTotype(QString);
    static User fromJson(QJsonObject);
    static QString RoleToString(Role r){
        QString typet;
        switch(r){
        case child:{
            typet = "Child";
            break;
        }
        case adult:{
            typet = "Adult";
            break;
        }
        case elderly:{
            typet = "Elderly";
            break;
        }
        case student:{
            typet = "Student";
            break;
        }
        default:{
            typet = "Unknown";
            break;
        }
        }
        return typet;
    }
    static QList<User> fromJsonList(QJsonObject json){
        QJsonArray data = json["data"].toArray();
        QList<User>list;
        for(int i=0;i<data.size();i++){
            QJsonObject person = data[i].toObject();
            User t;
            t.name = person["name"].toString();
            t.avatar = person["avatar"].toString();
            t.email = person["email"].toString();
            t.tel = person["tel"].toString();
            t.age = person["age"].toInt();
            t.id = person["id"].toString();
            t.idcard_num = person["idcard_num"].toString();
            t.join_at = QDate::fromString(person["join_at"].toString(),"yyyy-MM-dd");
            t.type = StringTotype(person["type"].toString());
            list.append(t);
        }
        return list;
    }
};
Q_DECLARE_METATYPE(User*)
Q_DECLARE_METATYPE(User)
#endif // USER_H
